package com.wlzg.collectiontool.view

import android.Manifest.permission.*
import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.github.dfqin.grantor.PermissionListener
import com.jaredrummler.materialspinner.MaterialSpinner
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.adapter.GuandianAdapter
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.*
import com.wlzg.collectiontool.util.*
import kotlinx.android.synthetic.main.guandian_dia.view.*
import kotlinx.android.synthetic.main.guandian_dia.view.baocun
import kotlinx.android.synthetic.main.guandian_dia.view.chakan
import kotlinx.android.synthetic.main.guandian_dia.view.quxiao
import kotlinx.android.synthetic.main.guandian_dia.view.shanchu
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*
import kotlin.collections.ArrayList

class GuandianDia(
    context: Context,
    var mFragment: InvestMapFragment,
    var t: Any? = null,
    var type: Int = 0
) :
    CenterPopupView(context), MaterialSpinner.OnItemSelectedListener<Any>, View.OnClickListener {
    private val TAG: String = "GuandianDia"
    private var sSleixinEnum: Constants.SSleixin? = null
    private var hunjieList: ArrayList<PsHunjie?>? = ArrayList()
    private val viewList: ArrayList<View> = ArrayList()
    private var mAdapter: GuandianAdapter = GuandianAdapter()
    private var imagePop: BasePopupView? = null
    var imageList: ArrayList<String> = ArrayList()
    var jImagelist: ArrayList<String> = ArrayList()
    var proList: ArrayList<PsXiangmu?>? = ArrayList()
    var gcList: ArrayList<PsGongcheng?>? = ArrayList()
    var wzList: ArrayList<Weizhi?>? = ArrayList()
    val sysAllDictItems = MConfigUtil.sysAllDictItems
    val manholeStyle = MConfigUtil.sysAllDictItems?.manhole_style
    var xsPosition: Int = 0
    var lbPosition: Int = 0;
    var czPosition: Int = 0;
    var jdxsPosition:Int =0;
    var jleixingPosition:Int=0;
    var pfkpmczPosition:Int=0
    var lgdmsjPosition:Int=0
    var lgsyPosition:Int=0
    var lgdmxsPosition:Int=0
    var glXmId = 0L;
    var glWzId = 0L;
    var ifxq = false
    var glGcId = 0L;
    var imageDia: ImageDia? = null
    private var ifqn = 0;
//    private var fdqn = 0;
    private var yskifqx = 0;
    private var yskifhj = 0
    private var yskifzz = 0
    private var pfkpm = 0
    private var lgifsp = 0
    private var lgifhj = 0
    private var jinType = 0
    private var pvTimeU: TimePickerUtil? = null
    private var timeFlag = -1
    override fun getImplLayoutId(): Int {
        return R.layout.guandian_dia
    }

    override fun onCreate() {
        super.onCreate()
        photo.setOnClickListener(this)
        baocun.setOnClickListener(this)
        chakan.setOnClickListener(this)
        close.setOnClickListener(this)
        jcjdate.setOnClickListener(this)
        yskdate.setOnClickListener(this)
        pfkdate.setOnClickListener(this)
        lgdate.setOnClickListener(this)
        hfcdate.setOnClickListener(this)
        shanchu.setOnClickListener(this)
        quxiao.setOnClickListener(this)
        pvTimeU = TimePickerUtil(context) {
            when (timeFlag) {
                0 -> jcjdate.text = it
                1 -> yskdate.text = it
                2 -> pfkdate.text = it
                3 -> lgdate.text = it
                4-> hfcdate.text=it
            }

        }
        imageDia = ImageDia(context, this)
        imagePop = XPopup.Builder(context).dismissOnTouchOutside(false)
            .offsetX(50)
            .offsetY(30)
            .asCustom(imageDia)
        initSpinner()
        initView()
        initItemListenner()
        initzb()
        t?.let { initData(it, type) }
    }
    private fun initzb(){
        jcjzx.text=mFragment.gdPoint?.x.toString()
        yskzx.text=mFragment.gdPoint?.x.toString()
        pfkzx.text=mFragment.gdPoint?.x.toString()
        lgzx.text=mFragment.gdPoint?.x.toString()
        hfczx.text=mFragment.gdPoint?.x.toString()

        jcjzy.text=mFragment.gdPoint?.y.toString()
        yskzy.text=mFragment.gdPoint?.y.toString()
        pfkzy.text=mFragment.gdPoint?.y.toString()
        lgzy.text=mFragment.gdPoint?.y.toString()
        hfczy.text=mFragment.gdPoint?.y.toString()
    }

    private fun initItemListenner() {
        mAdapter.addChildClickViewIds(
            R.id.baocun1,
            R.id.delete,
            R.id.quxiao,
            R.id.paizhao1,
            R.id.paizhao2,
            R.id.paizhao3,
            R.id.chakan
        )
        mAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
                when (view.id) {
                    R.id.baocun1 -> {

                    }
                    R.id.delete -> {
                        hunjieList?.removeAt(position)
                        mAdapter.notifyDataSetChanged()

                    }
                    R.id.quxiao -> {
                        hunjieList?.removeAt(position)
                        mAdapter.notifyDataSetChanged()
                    }
                    R.id.paizhao1 -> {
                        photo(1, position)

                    }
                    R.id.paizhao2 -> {
                        photo(2, position)

                    }
                    R.id.paizhao3 -> {
                        photo(3, position)

                    }
                    R.id.chakan -> {
                        if (!imagePop?.isShow!!) {
                            hunjieList?.get(position)?.image1?.let { jImagelist.add(it) }
                            hunjieList?.get(position)?.image2?.let { jImagelist.add(it) }
                            hunjieList?.get(position)?.image3?.let { jImagelist.add(it) }
                            imageDia?.imageList = jImagelist
                            imagePop?.show()
                        }
                    }

                }
            }

        })
    }

    private fun initView() {

    }

    private fun initGlxm(t: BaseGuandian) {
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            var xm: PsXiangmu?
            var gc: PsGongcheng?
            var wz: Weizhi?
            withContext(Dispatchers.IO) {
                proList =
                    AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>
                gcList = AppDatabase.getInstance().userDao()
                    .getGcList() as ArrayList<PsGongcheng?>
                xm = AppDatabase.getInstance().userDao().getXmListbyId(t.glxmId)
                gc = AppDatabase.getInstance().userDao().getGcListByid(t.glgcId)
                if (t.ifxq) {
                    val xq = AppDatabase.getInstance().userDao().getxqbyId(t.glwzId)
                    wz = Weizhi()
                    wz?.id = xq?.xiaoquId
                    wz?.name = xq?.villageName

                } else {
                    val dl = AppDatabase.getInstance().userDao().getDlbyId(t.glwzId)
                    wz = Weizhi()
                    wz?.id = dl?.daoluId
                    wz?.name = dl?.roadName
                }
                val xqlist = AppDatabase.getInstance().userDao().getxqList()
                val dllist = AppDatabase.getInstance().userDao().getdlList()
                wzList?.clear()
                xqlist?.let {
                    for (xq in it) {
                        val wz = Weizhi()
                        wz.id = xq?.xiaoquId
                        wz.name = xq?.villageName
                        wz.ifxq = true
                        wzList?.add(wz)
                    }
                }
                dllist?.let {
                    for (dl in it) {
                        val wz = Weizhi()
                        wz.id = dl?.daoluId
                        wz.name = dl?.roadName
                        wz.ifxq = false
                        wzList?.add(wz)
                    }
                }

            }
            if (proList?.size!! > 0) {
                glxm.setItems(proList as ArrayList<PsXiangmu?>)
                glxm.setOnItemSelectedListener(this@GuandianDia)
                glxm.selectedIndex = proList?.indexOf(xm)!!
            }
            if (gcList?.size!! > 0) {
                glgc.setItems(gcList as ArrayList<PsGongcheng?>)
                glgc.setOnItemSelectedListener(this@GuandianDia)
                glgc.selectedIndex = gcList?.indexOf(gc)!!
            }
            if (wzList?.size!! > 0) {
                glwz.setItems(wzList as ArrayList<Weizhi>)
                glwz.setOnItemSelectedListener(this@GuandianDia)
                glwz.selectedIndex = wzList?.indexOf(wz)!!
            }
        }
    }

    fun initData(t: Any, type: Int) {
        when (type) {
            1 -> {
                t as PsJin
//                glxm.selectedIndex=t.xmIndex
//                glgc.selectedIndex=t.gcIndex
//                glwz.selectedIndex=t.wzIndex
                ssleixing.selectedIndex = t.spinnerIndex
                switchLl(0)
              ssleixing.isClickable=false
                jcjBh.setText(t.manholecode)
                jgChicun.setText(t.covDimen1.toString())
                jChicun.setText(t.chamberHeight.toString())
                jshenmai.setText(t.depth.toString())
//                jdxingshi.setText(t.bottomStyle.toString())
                jcjzx.setText(t.x1Coor.toString())
                jcjzy.setText(t.y1Coor.toString())
                jcjbeizhu.setText(t.remark)
                zengbu.setText(t.remark2)
                jcjdate.setText(t.surveyDate)
                jxinshi.selectedIndex = t.manholeStyle!!
                jleibie.selectedIndex = t.manholeCategory!!
                jdxingshi.selectedIndex=t.bottomStyle!!
                jleixing.selectedIndex= t.manholeType!!

                ifqinang.isChecked = t.airbag == 1
                jfdqlgj.setText(t.airbagData)
//                fdql.isChecked = t.airbagData.equals("1")
                initGlxm(t)

//                jcaizhi.selectedIndex = t.covMaterial!!
                t.iamgeList?.let {
                    imageList = it as ArrayList<String>
                }
                hunjieList = t.hjList as ArrayList<PsHunjie?>
                mAdapter.setNewInstance(hunjieList)
                mAdapter.notifyDataSetChanged()

            }
            2 -> {
                t as Psyushuikou
                ssleixing.selectedIndex = 1
                switchLl(1)
                ssleixing1.isClickable=false
                yskbh.setText(t.combcode)
                yskxinshi.selectedIndex = t.combType
                yskcaizhi.selectedIndex = t.graMateri
                yskchicun.setText(t.combDimen1.toString())
                yskmaxdepth.setText(t.maxdepth.toString())
//                yskweizhi.setText(t.roadName)
                yskzx.setText(t.x1Coor.toString())
                yskzy.setText(t.y1Coor.toString())
                yskdate.setText(t.reportDate)
                yskifzhuangz.isChecked = t.antitrash == 1
                yskbeizhu.setText(t.remark)
                initGlxm(t)
                t.iamgeList?.let {
                    imageList = it as ArrayList<String>
                }
                hunjieList = t.hjList as ArrayList<PsHunjie?>
                mAdapter.setNewInstance(hunjieList)
                mAdapter.notifyDataSetChanged()

            }
            3 -> {
                t as Pspfk
                ssleixing.selectedIndex = 2
                switchLl(2)
                ssleixing2.isClickable=false
                pfkbh.setText(t.outfallcode)
                pfkleibie.selectedIndex = t.outfallCa
                pfkpmcz.selectedIndex=t.flapMater
//                pfkwz.setText(t.addr)
                pfkpaimen.isChecked = t.flap == 1
                pfksnst.setText(t.receivewat)
                pfkname.setText(t.name)
                pfkpmzj.setText(t.flapDiame.toString())
                pfkpmheight.setText(t.flapTopel.toString())
                pfkpmheightb.setText(t.flapBotel.toString())
                pfkzx.setText(t.x1Coor.toString())
                pfkzy.setText(t.y1Coor.toString())
                pfkdate.setText(t.reportDate)
                pfkjxbm.setText(t.pipecode)
                pfkbeizhu.setText(t.remark)


            }
            4 -> {
                t as PsLiguan
                ssleixing.selectedIndex = 3
                switchLl(3)
                ssleixing3.isClickable=false
                lgbh.setText(t.risercode)
                lgleibie.selectedIndex = t.riserCategory
                lgcaizhi.selectedIndex = t.riserMat
                lgdmsj1.setText(t.riserDia1.toString())
                lgsy.selectedIndex=t.riserSour
                lgdmxs.selectedIndex=t.riserShap
                lgdmsj2.setText(t.riserDia2.toString())
                lgpfk.setText(t.outfallcode)
                lgifhunjie.isChecked = t.rainsewamix == 1
                lgifsanpai.isChecked = t.riserDir == 1
                lgzx.setText(t.x1Coor.toString())
                lgzy.setText(t.y1Coor.toString())
                lgzz.setText(t.riserDownElev.toString())
                //位置？日期？
                lgbeizhu.setText(t.remark)

            }
            5 -> {
                t as PsHuafenchi
                ssleixing.selectedIndex = 4
                switchLl(4)
                ssleixing4.isClickable=false
                hfcbh.setText(t.septictacode)
//                hfcwz.setText()位置
                hfcname.setText(t.name)
                hfcjbh.setText(t.manholecode)
                hfccc.setText(t.septictankSize)
                hfcdz.setText(t.addr)
                hfcdate.setText(t.reportDate)
                hfczx.setText(t.x1Coor.toString())
                hfczy.setText(t.y1Coor.toString())
                //hfczz.setText() 高度
                hfcbeizhu.setText(t.remark)

            }
        }
    }


    private fun initSpinner() {
        ifqinang.setOnCheckedChangeListener { view, isChecked ->
            ifqn = if (isChecked) 1 else 0
        }
//        fdql.setOnCheckedChangeListener { view, isChecked ->
//            fdqn = if (isChecked) 1 else 0
//        }
        ifhunjie.setOnCheckedChangeListener { view, isChecked ->
            yskifhj = if (isChecked) 1 else 0
        }
        ifquexian.setOnCheckedChangeListener { view, isChecked ->
            yskifqx = if (isChecked) 1 else 0
        }
        yskifzhuangz.setOnCheckedChangeListener { view, isChecked ->
            yskifzz = if (isChecked) 1 else 0
        }
        pfkpaimen.setOnCheckedChangeListener { view, isChecked ->
            pfkpm = if (isChecked) 1 else 0

        }
        lgifsanpai.setOnCheckedChangeListener { view, isChecked ->
            lgifsp = if (isChecked) 1 else 0
        }
        lgifhunjie.setOnCheckedChangeListener { view, isChecked ->
            lgifhj = if (isChecked) 1 else 0
        }
        lgifhunjie.setOnCheckedChangeListener { view, isChecked -> }
        ssleixing.setItems("检查井", "雨水口", "排水口", "立管", "化粪池")
        ssleixing.selectedIndex = 0
        sSleixinEnum = Constants.SSleixin.JIANCHAJIN
        ssleixing.setOnItemSelectedListener(this)
        ssleixing1.setItems("检查井", "雨水口", "排水口", "立管", "化粪池")
        ssleixing1.setOnItemSelectedListener(this)
        ssleixing2.setItems("检查井", "雨水口", "排水口", "立管", "化粪池")
        ssleixing2.setOnItemSelectedListener(this)
        ssleixing3.setItems("检查井", "雨水口", "排水口", "立管", "化粪池")
        ssleixing3.setOnItemSelectedListener(this)
        ssleixing4.setItems("检查井", "雨水口", "排水口", "立管", "化粪池")
        ssleixing4.setOnItemSelectedListener(this)
        jxinshi.setItems(MConfigUtil.sysAllDictItems?.manhole_style as ArrayList)
        jxinshi.setOnItemSelectedListener(this)
        jleibie.setItems(MConfigUtil.sysAllDictItems?.manholeCategory as ArrayList)
        jleibie.setOnItemSelectedListener(this)
        jcaizhi.setItems(MConfigUtil.sysAllDictItems?.manholeMaterial as ArrayList)
        jcaizhi.setOnItemSelectedListener(this)
        jleixing.setItems(MConfigUtil.sysAllDictItems?.manhole_type as ArrayList)
        jleixing.setOnItemSelectedListener(this)
        jdxingshi.setItems("平底","流槽","落底","其它")
        jdxingshi.setOnItemSelectedListener(this)
        yskxinshi.setItems(MConfigUtil.sysAllDictItems?.combtype as ArrayList)
        yskxinshi.setOnItemSelectedListener(this)
        yskcaizhi.setItems(MConfigUtil.sysAllDictItems?.material as ArrayList)
        yskxinshi.setOnItemSelectedListener(this)
        pfkleibie.setItems(MConfigUtil.sysAllDictItems?.outfallCa as ArrayList)
        pfkleibie.setOnItemSelectedListener(this)
        pfkpmcz.setItems("铸铁","钢","不锈钢","塑料","复合材料","其它")
        pfkpmcz.setOnItemSelectedListener(this)
        lgleibie.setItems("雨水","污水","合流")
        lgleibie.setOnItemSelectedListener(this)
        lgcaizhi.setItems("PE（聚乙烯）管","HDPE（高密度聚乙烯）管","UPVC管","铸铁管","玻璃钢夹砂管","钢管","其他")
        lgcaizhi.setOnItemSelectedListener(this)
//        lgdmsj1.setItems("圆形填写直径","矩形填写较长边")
//        lgdmsj1.setOnItemSelectedListener(this)
        lgsy.setItems("洗衣机","空调","厨房","卫生间","地漏","雨水","其他",)
        lgsy.setOnItemSelectedListener(this)
        lgdmxs.setItems("圆形","矩形","其他")
        lgdmxs.setOnItemSelectedListener(this)

        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                proList = AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>
                if(proList==null){
                    proList= ArrayList()
                }
                if(proList!!.size>0){
                    val id= proList?.get(0)?.xiangmuId
                    gcList = id?.let {
                        AppDatabase.getInstance().userDao()
                            .getGcListBypid(it)
                    } as ArrayList<PsGongcheng?>
                }
                if(gcList==null){
                    gcList= ArrayList()
                }
                val xqlist = AppDatabase.getInstance().userDao().getxqList()
                val dllist = AppDatabase.getInstance().userDao().getdlList()
                wzList?.clear()
                xqlist?.let {
                    for (xq in it) {
                        val wz = Weizhi()
                        wz.id = xq?.xiaoquId
                        wz.name = xq?.villageName
                        wz.ifxq = true
                        wzList?.add(wz)
                    }
                }
                dllist?.let {
                    for (dl in it) {
                        val wz = Weizhi()
                        wz.id = dl?.daoluId
                        wz.name = dl?.roadName
                        wz.ifxq = false
                        wzList?.add(wz)
                    }
                }
            }
            if (proList?.size!! > 0) {
                glxm.setItems(proList as ArrayList<PsXiangmu?>)
                glXmId = proList?.get(0)?.xiangmuId!!
            }
            glxm.setOnItemSelectedListener(this@GuandianDia)
            if (gcList?.size!! > 0) {
                glgc.setItems(gcList as ArrayList<PsGongcheng?>)
                glGcId = gcList?.get(0)?.gcId!!
            }
            glgc.setOnItemSelectedListener(this@GuandianDia)
            if (wzList?.size!! > 0) {
                glwz.setItems(wzList as ArrayList<Weizhi>)
                glWzId = wzList?.get(0)?.id!!
            }
            glwz.setOnItemSelectedListener(this@GuandianDia)

        }
//        caizhi1.setItems(MConfigUtil.sysAllDictItems?.gr)


        addh.setOnClickListener(this)
        var ll = FullyLinearLayoutManager(
            context,
            LinearLayoutManager.VERTICAL,
            false
        )
        ll.isSmoothScrollbarEnabled = true
        ll.isAutoMeasureEnabled = true
        recyclerView.layoutManager = ll
        recyclerView.adapter = mAdapter
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false

        mAdapter.setNewData(hunjieList)

    }

    override fun onItemSelected(view: MaterialSpinner?, position: Int, id: Long, item: Any?) {
        when (view?.id) {
            R.id.ssleixing -> {
                switchLl(position)
            }
            R.id.ssleixing1 -> {
                switchLl(position)
            }
            R.id.ssleixing2 -> {
                item?.let {
                    switchLl(position)
                }
            }
            R.id.ssleixing3 -> {
                switchLl(position)
            }
            R.id.ssleixing4 -> {
                switchLl(position)
            }
            R.id.jxinshi -> {
                val mitem = item as? LoginRes.SysAllDictItems.OlFormBizType
                mitem?.text
                xsPosition = position
                jinType = position

            }
            R.id.jleibie -> {
                lbPosition = position

            }
            R.id.jleixing -> {
                jleixingPosition = position

            }
            R.id.jcaizhi -> {
                czPosition = position

            }
            R.id.jdxingshi -> {
                jdxsPosition = position

            }
            R.id.yskxinshi -> {
                val mitem = item as? LoginRes.SysAllDictItems.OlFormBizType
                mitem?.text
                xsPosition = position

            }
            R.id.yskcaizhi -> {
                val mitem = item as? LoginRes.SysAllDictItems.OlFormBizType
                mitem?.text
                czPosition = position

            }
            R.id.pfkleibie -> {
                val mitem = item as? LoginRes.SysAllDictItems.OlFormBizType
                lbPosition = position

            }
            R.id.pfkpmcz -> {
                pfkpmczPosition = position

            }


            R.id.lgleibie -> {
                lbPosition = position

            }
            R.id.lgcaizhi -> {
                czPosition = position

            }
            R.id.lgsy -> {
                lgsyPosition = position

            }
            R.id.lgdmxs -> {
                lgdmxsPosition = position

            }




            R.id.glxm -> {
                mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
                    item as PsXiangmu
                    glXmId = item.xiangmuId!!
                    withContext(Dispatchers.IO) {
                        gcList = AppDatabase.getInstance().userDao()
                            .getGcListBypid(item.xiangmuId!!) as ArrayList<PsGongcheng?>
                    }
                    if (gcList == null) {
                        gcList=ArrayList()
                    }
                    glgc.setItems(gcList as ArrayList<PsGongcheng>)

                }


            }
            R.id.glgc -> {
                item as PsGongcheng
                glGcId = item.gcId!!
                Log.d("YYHH", "id:" + glGcId)

            }
            R.id.glwz -> {
                item as Weizhi
                glWzId = item.id!!
                ifxq = item.ifxq

            }


        }
    }


    private fun switchLl(position: Int) {
        when (position) {
            0 -> {
                sSleixinEnum = Constants.SSleixin.JIANCHAJIN
                lltype.visibility = VISIBLE
                ssleixing.selectedIndex = position
                ssleixing1.selectedIndex = position
                ssleixing2.selectedIndex = position
                ssleixing3.selectedIndex = position
                ssleixing4.selectedIndex = position
                lltype1.visibility = GONE
                lltype2.visibility = GONE
                lltype3.visibility = GONE
                lltype4.visibility = GONE
            }
            1 -> {
                sSleixinEnum = Constants.SSleixin.YUSHUIKOU
                lltype1.visibility = VISIBLE
                ssleixing.selectedIndex = position
                ssleixing1.selectedIndex = position
                ssleixing2.selectedIndex = position
                ssleixing3.selectedIndex = position
                ssleixing4.selectedIndex = position
                lltype.visibility = GONE
                lltype2.visibility = GONE
                lltype3.visibility = GONE
                lltype4.visibility = GONE
            }
            2 -> {
                sSleixinEnum = Constants.SSleixin.PAISHUIKOU
                lltype2.visibility = VISIBLE
                ssleixing.selectedIndex = position
                ssleixing1.selectedIndex = position
                ssleixing2.selectedIndex = position
                ssleixing3.selectedIndex = position
                ssleixing4.selectedIndex = position
                lltype1.visibility = GONE
                lltype.visibility = GONE
                lltype3.visibility = GONE
                lltype4.visibility = GONE
                addh.visibility = View.INVISIBLE
            }
            3 -> {
                sSleixinEnum = Constants.SSleixin.LIGUAN
                lltype3.visibility = VISIBLE
                ssleixing.selectedIndex = position
                ssleixing1.selectedIndex = position
                ssleixing2.selectedIndex = position
                ssleixing3.selectedIndex = position
                ssleixing4.selectedIndex = position
                lltype.visibility = GONE
                lltype2.visibility = GONE
                lltype1.visibility = GONE
                lltype4.visibility = GONE
                addh.visibility = View.INVISIBLE
            }
            4 -> {
                sSleixinEnum = Constants.SSleixin.HUAFENCHI
                lltype4.visibility = VISIBLE
                ssleixing.selectedIndex = position
                ssleixing1.selectedIndex = position
                ssleixing2.selectedIndex = position
                ssleixing3.selectedIndex = position
                ssleixing4.selectedIndex = position
                lltype.visibility = GONE
                lltype2.visibility = GONE
                lltype1.visibility = GONE
                lltype3.visibility = GONE
                addh.visibility = View.INVISIBLE
            }
        }

    }

    override fun getMaxWidth(): Int {
        return 0
    }


    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.addh -> {
                if (hunjieList?.size!! <= 0) {
                    hunjieList?.add(PsHunjie())
                    mAdapter.setNewInstance(hunjieList)
                } else {
                    val psHunjie = hunjieList!![hunjieList!!.size - 1] as PsHunjie
                    hunjieList?.add(psHunjie.copy())

                }
                mAdapter.notifyDataSetChanged()


            }
            R.id.photo -> {
                photo(0, 0)
            }
            R.id.chakan -> {
                if (!imagePop?.isShow!!) {
                    imageDia?.imageList = imageList
                    imagePop?.show()
                }

            }
            R.id.baocun -> {
                val obj = getText()
                val isUpdate: Boolean = t == null
                sSleixinEnum?.let {
                    obj?.let { it1 -> mFragment.onSave(it, it1, !isUpdate, jinType) }
                }
                dismiss()
            }
            R.id.close -> {
                if(t==null){
                    sSleixinEnum?.let {
                        mFragment.ondelete(it,null)
                    }
                }
                dismiss()
            }
            R.id.shanchu->{
                sSleixinEnum?.let {
                    mFragment.ondelete(it,t)

                }
            }
            R.id.quxiao->{
                if(t==null){
                sSleixinEnum?.let {
                    mFragment.ondelete(it,null)
                }
                }
                dismiss()
            }
            R.id.jcjdate -> {
                timeFlag = 0
                pvTimeU?.pvTime?.show()
            }
            R.id.yskdate -> {
                timeFlag = 1
                pvTimeU?.pvTime?.show()
            }
            R.id.pfkdate -> {
                timeFlag = 2
                pvTimeU?.pvTime?.show()
            }
            R.id.lgdate -> {
                timeFlag = 3
                pvTimeU?.pvTime?.show()
            }
            R.id.hfcdate -> {
                timeFlag = 4
                pvTimeU?.pvTime?.show()
            }

        }
    }

    private fun saveCommon(t: BaseGuandian) {
        t.glxmId = glXmId
        t.glgcId = glGcId
        t.glwzId = glWzId
        t.ifxq = ifxq
        t.iamgeList = imageList
    }

    private fun getText(): Any? {
        var obj: Any? = null
        when (sSleixinEnum) {
            Constants.SSleixin.JIANCHAJIN -> {
                var psJin: PsJin = if (t != null) {
                    t as PsJin
                } else {
                    PsJin()
                }
                saveCommon(psJin)
                psJin.spinnerIndex = 0
                psJin.manholecode = jcjBh.text.toString()
                try {
                    psJin.covDimen1 = jgChicun.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    psJin.depth = jshenmai.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                psJin.airbag = ifqn
                psJin.airbagData = jfdqlgj.text.toString()
                try {
                    psJin.chamberHeight = jChicun.text.toString().toInt()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
//                try {
//                    psJin.chamberLength = jshenmai.text.toString().toInt()
//                } catch (e: NumberFormatException) {
//                    e.printStackTrace()
//                }
//                try {
//                    psJin.bottomStyle = jChicun.text.toString().toInt()
//                } catch (e: NumberFormatException) {
//                    e.printStackTrace()
//                }
                try {
                    psJin.x1Coor = jcjzx.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    psJin.y1Coor = jcjzy.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                psJin.remark = jcjbeizhu.text.toString()
                psJin.remark2 = zengbu.text.toString()
                psJin.surveyDate = jcjdate.text.toString()
                psJin.manholeStyle = xsPosition
                psJin.manholeType=jleixingPosition
                psJin.manholeCategory = lbPosition
                psJin.bottomStyle=jdxsPosition
                psJin.covMaterial = czPosition
                psJin.hjList = hunjieList

                psJin.objectid = 12
                return psJin

            }
            Constants.SSleixin.YUSHUIKOU -> {
                var yushuikou: Psyushuikou = if (t != null) {
                    t as Psyushuikou
                } else {
                    Psyushuikou()
                }
                saveCommon(yushuikou)
                yushuikou.combcode = yskbh.text.toString()
                yushuikou.combType = xsPosition
                yushuikou.graMateri = czPosition
                try {
                    yushuikou.combDimen1 = yskchicun.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    yushuikou.maxdepth = yskmaxdepth.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
//                yushuikou.roadName = yskweizhi.text.toString()
                try {
                    yushuikou.x1Coor = yskzx.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    yushuikou.y1Coor = yskzy.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                yushuikou.reportDate = yskdate.text.toString()
                yushuikou.antitrash = yskifzz
                yushuikou.remark = yskbeizhu.text.toString()
                yushuikou.remark2 = zengbu.text.toString()
                yushuikou.hjList = hunjieList
                return yushuikou
            }

            Constants.SSleixin.PAISHUIKOU -> {
                var pfk: Pspfk = if (t != null) {
                    t as Pspfk
                } else {
                    Pspfk()
                }
                saveCommon(pfk)
                pfk.outfallcode = pfkbh.text.toString()
                pfk.outfallCa = lbPosition
                pfk.flapMater = pfkpmczPosition
//                pfk.addr = pfkwz.text.toString()
                pfk.flap = pfkpm
                pfk.receivewat = pfksnst.text.toString()
                pfk.name = pfkname.text.toString()
                try {
                    pfk.flapDiame = pfkpmzj.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    pfk.flapTopel = pfkpmheight.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    pfk.flapBotel = pfkpmheightb.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    pfk.x1Coor = pfkzx.text.toString().toDouble()
                    pfk.y1Coor = pfkzy.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                pfk.reportDate = pfkdate.text.toString()
                pfk.pipecode = pfkjxbm.text.toString()
                pfk.remark = pfkbeizhu.text.toString()
                pfk.remark2 = zengbu.text.toString()
                return pfk
            }

            Constants.SSleixin.LIGUAN -> {
                var lg: PsLiguan = if (t != null) {
                    t as PsLiguan
                } else {
                    PsLiguan()
                }
                saveCommon(lg)
                lg.risercode = lgbh.text.toString()
                lg.lbPosition = lbPosition
                lg.czPosition = czPosition
//                lg.riserDia1=lgdmsjPosition
                lg.riserMat=czPosition
                lg.riserSour=lgsyPosition
                lg.riserShap=lgdmxsPosition
                lg.riserCategory=lbPosition
                try {
                    lg.riserDia1 = lgdmsj1.text.toString()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                lg.riserDia2 = lgdmsj2.text.toString()
                lg.rainsewamix = lgifhj
                lg.riserDir = lgifsp
                try {
                    lg.x1Coor = lgzx.text.toString().toDouble()
                    lg.y1Coor = lgzy.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                try {
                    lg.riserDownElev = lgzz.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                //位置日期？
                lg.remark = lgbeizhu.text.toString()
                lg.remark2 = zengbu.text.toString()
                return lg

            }
            Constants.SSleixin.HUAFENCHI -> {
                var hfc: PsHuafenchi = if (t != null) {
                    t as PsHuafenchi
                } else {
                    PsHuafenchi()
                }
                saveCommon(hfc)
                hfc.septictacode = hfcbh.text.toString()
                hfc.name = hfcname.text.toString()
                hfc.manholecode = hfcjbh.text.toString()
                hfc.septictankSize = hfccc.text.toString()
                hfc.addr = hfcdz.text.toString()
                hfc.reportDate = hfcdate.text.toString()
                try {
                    hfc.x1Coor = hfczx.text.toString().toDouble()
                    hfc.y1Coor = hfczy.text.toString().toDouble()
                } catch (e: NumberFormatException) {
                    e.printStackTrace()
                }
                hfc.remark = hfcbeizhu.text.toString()
                hfc.remark2 = zengbu.text.toString()
                return hfc
            }
        }

        return obj
    }

    private fun photo(type: Int, position: Int?) {
        PermissionUtil.requestPermission(
            context as Activity?, arrayOf<String>(
                ACCESS_FINE_LOCATION,
                WRITE_EXTERNAL_STORAGE,
                READ_EXTERNAL_STORAGE,
                CAMERA

            ), object :
                PermissionListener {
                override fun permissionGranted(permission: Array<out String>) {
                    PictureSelector.create(mFragment)
                        .openCamera(PictureMimeType.ofImage())
                        .imageEngine(GlideEngine.createGlideEngine())
                        .forResult(object : OnResultCallbackListener<LocalMedia?> {
                            override fun onResult(result: List<LocalMedia?>) {
                                result[0]?.path?.let { Log.d("testMM", it) }
                                result[0]?.realPath?.let { imageList.add(it) }
                                result[0]?.compressPath?.let { Log.d("testMMss", it) }
                                when (type) {
                                    0 -> {
                                        result[0]?.realPath?.let { imageList.add(it) }
                                    }
                                    1 -> {
                                        position?.let {
                                            hunjieList?.get(it)?.image1 = result[0]?.realPath
                                            mAdapter.notifyDataSetChanged()
                                        }
                                    }
                                    2 -> {
                                        position?.let {
                                            hunjieList?.get(it)?.image2 = result[0]?.realPath
                                            mAdapter.notifyDataSetChanged()
                                        }
                                    }
                                    3 -> {
                                        position?.let {
                                            hunjieList?.get(it)?.image3 = result[0]?.realPath
                                            mAdapter.notifyDataSetChanged()
                                        }
                                    }


                                }


                            }

                            override fun onCancel() {
                                // 取消
                            }
                        })

                }

                override fun permissionDenied(permission: Array<out String>) {
                    for (i in permission) {
                        Log.d(TAG, i)
                    }
                }
            })

    }


}